package com.jxnu.escms.Service.InOutService.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jxnu.escms.Dao.InOutDao.InOutDao;
import com.jxnu.escms.Pojo.InOutRecord;
import com.jxnu.escms.Pojo.PageResult;
import com.jxnu.escms.Service.InOutService.InOutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class InOutSerImpl implements InOutService {

    @Resource
    private InOutDao inOutDao;

    @Override
    public int addApply(Map<String, Object> data) {
        return inOutDao.insertApplication(data);
    }

    @Override
    public PageResult<InOutRecord> queryRecord(int id, int start, int pageSize) {
        PageHelper.startPage(start, pageSize);

        Page<InOutRecord> allById = (Page<InOutRecord>)inOutDao.getAllById(id);
        for(InOutRecord p : allById){
            Integer checkerId = p.getCheckerId();
            p.setChecker(inOutDao.getAdmin(checkerId));
        }

        return new PageResult<InOutRecord>(Math.toIntExact(allById.getTotal()), allById);
    }

    @Override
    public int deleteRecord(int userId, String time) {
        return inOutDao.deleteRecord(userId, time);
    }

    @Override
    public PageResult<InOutRecord> getRecords(int adminId, int start, int pageSize, int kill) {
        PageHelper.startPage(start, pageSize);

        Page<InOutRecord> allById = (Page<InOutRecord>)inOutDao.getAllByAdminId(adminId, kill);

        return new PageResult<>(Math.toIntExact(allById.getTotal()), allById);
    }

    @Override
    public int auditRecord(int id, String result) {
        return inOutDao.updateResult(id, result);
    }

    @Override
    public int addControlRecord(int adminId, int record, String type) {
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = simpleFormatter.format(new Date());
        return inOutDao.insertRecord(adminId, record, type, date);
    }

    @Override
    public List<InOutRecord> filterRecords(int adminId, int kill) {
        return inOutDao.getAllByAdminId(adminId, kill);
    }

    @Override
    public int deleteById(int id) {
        return inOutDao.deleteById(id);
    }
}
